import assetsJson from './assets';
import assetsPcJson from './assets_pc';
import assetsPcNext from './assets_pc_next';
import { DocFrameType, LcProject } from '../types/editor.type';
import { IPublicTypeAssetsJson } from '@alilc/lowcode-types';
import { loadProjectAssets } from './loadProjectAssets';
import { getOEMConfig } from '../../oem/oemConfig';

export const getProjectAssetsJson = (project: LcProject, frame: DocFrameType) => {
  let __assets = getDefaultAssets(project, frame);
  if (project?.assets) {
    __assets = loadProjectAssets(__assets, JSON.parse(JSON.stringify(project?.assets)), frame);
  }
  return __assets;
};

export const getDefaultAssets = function (project, frame) {
  const assets = JSON.parse(JSON.stringify(assetsJson));
  const assetsPc = JSON.parse(JSON.stringify(assetsPcJson));
  let __assets: IPublicTypeAssetsJson = assets;
  const { theme, mainComponent } = project;
  getCrossUI(__assets, frame);
  if (frame === 'web') {
    if (mainComponent === 'fusion') {
      __assets = assetsPcNext as any;
      __assets.packages = assets.packages.concat(__assets.packages);
      __assets.components = __assets.components.concat(assets.components);
    } else {
      if (theme === 'dark') {
        // console.log('[assetsPc.packages] ', assetsPc.packages);
        // @ts-ignore
        assetsPc.packages[5].urls[2] = `https://${
          getOEMConfig().cdnDomain
        }/editor/antd/antd.dark.min.css`;
        assetsPc.packages[5].editUrls[2] = `https://${
          getOEMConfig().cdnDomain
        }/editor/antd/antd.dark.min.css`;
      }
      // @ts-ignore
      assets.packages = assets.packages.concat(assetsPc.packages);
      // @ts-ignore
      assets.components = assetsPc.components.concat(assets.components);
    }
  }
  return __assets;
};

export const getCrossUI = (assets, type) => {
  const crossUiPackage = type === 'web' ? '@disscode/react' : 'cross-ui';
  const crossUiLibrary = type === 'web' ? 'Disscode' : 'BizComps';
  const crossUiMeta = type === 'web' ? 'DisscodeReactMeta' : 'CrossUiMeta';
  const isDev = process.env.DEV_CROSS_UI === 'all';
  const crossUiVersion = '4.0.44';
  const disscodeReactVersion = '1.0.14';
  const version = type === 'web' ? disscodeReactVersion : crossUiVersion;
  const urls = isDev
    ? ['http://127.0.0.1:3333/view.js']
    : [
        `https://${getOEMConfig().cdnDomain}/material/117987/${crossUiPackage}/${version}/view.js`,
        `https://${getOEMConfig().cdnDomain}/material/117987/${crossUiPackage}/${version}/view.css`,
      ];
  const editUrls = isDev
    ? urls
    : [
        `https://${getOEMConfig().cdnDomain}/material/117987/${crossUiPackage}/${version}/view.js`,
        `https://${getOEMConfig().cdnDomain}/material/117987/${crossUiPackage}/${version}/view.css`,
      ];
  const url = isDev
    ? 'http://127.0.0.1:3333/meta.js'
    : `https://${getOEMConfig().cdnDomain}/material/117987/${crossUiPackage}/${version}/meta.js`;
  assets.packages.push({
    package: crossUiPackage,
    version,
    library: crossUiLibrary,
    urls,
    editUrls,
    advancedUrls: {
      default: urls,
    },
    advancedEditUrls: {},
  } as any);
  assets.components.push({
    exportName: crossUiMeta,
    npm: {
      package: crossUiPackage,
      version,
    },
    url,
    urls: {
      default: url,
    },
    advancedUrls: {
      default: [url],
    },
  });
  return assets;
};
